CertNameToStr (crypt32)
Last changed: -195.13.239.147

.
Summary
The CertNameToStr function converts an encoded name in a CERT_NAME_BLOB structure to a null-terminated character string.

C# Signature:

[DllImport("crypt32.dll", SetLastError=true)]
static extern TODO CertNameToStr(TODO);

VB Signature:

Declare Function CertNameToStr Lib "crypt32.dll" (TODO) As TODO

User-Defined Types:

public struct CERT_NAME_BLOB
{
     public uint cbData;
     public IntPtr pbData;
}

Alternative Managed API:

Do you know one? Please contribute it!

Notes:

None.

Tips & Tricks:

Please add some!

Sample Code:

    private static readonly uint X509_ASN_ENCODING = 1;

    private static readonly uint CERT_SIMPLE_NAME_STR = 1;
    private static readonly uint CERT_OID_NAME_STR = 2;
    private static readonly uint CERT_NAME_STR_CRLF_FLAG = 0x08000000;
    private static readonly uint CERT_NAME_STR_NO_QUOTING_FLAG = 0x10000000;
    private static readonly uint CERT_NAME_STR_REVERSE_FLAG = 0x02000000;

    [DllImport("crypt32.dll", SetLastError = true, EntryPoint = "CertNameToStr")]
    private static extern uint _certNameToStr(uint dwCertEncodingType, IntPtr pName, uint dwStrType, StringBuilder psz, uint csz);

    public static string CertNameToStr(byte[] certName)
    {
        uint dwStrType = CERT_SIMPLE_NAME_STR | CERT_OID_NAME_STR | CERT_NAME_STR_CRLF_FLAG | CERT_NAME_STR_NO_QUOTING_FLAG | CERT_NAME_STR_REVERSE_FLAG;
        StringBuilder psz = null;

        CERT_NAME_BLOB name;
        name.pbData = Marshal.AllocHGlobal(certName.Length);
        Marshal.Copy(certName, 0, name.pbData, certName.Length);
        name.cbData = (uint)certName.Length;

        IntPtr pName = Marshal.AllocHGlobal(certName.Length);
        Marshal.StructureToPtr(name, pName, false);

        try
        {
            uint ret = _certNameToStr(X509_ASN_ENCODING, pName, dwStrType, psz, 0);
            if (ret == 0) return null;

            psz = new StringBuilder((int)ret);
            ret = _certNameToStr(X509_ASN_ENCODING, pName, dwStrType, psz, ret);
        }
        finally
        {
            Marshal.FreeHGlobal(name.pbData);
            Marshal.FreeHGlobal(pName);
        }

        return psz.ToString();
    }

Documentation